package com.gopay.bis.cmbc.expay.operation;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Socket请求类
 * 
 * @SocketTransport.java
 * @author LEO
 * @2012-8-8 上午10:44:11 www.gopay.com.cn Inc.All rights reserved.
 */
public class SocketTransport {
    protected Log logger = LogFactory.getLog(this.getClass());

    /** socket客户端 ***/
//    private Socket socketClient = null;

    // 请求地址
    private String host;
    // 请求端口
    private int port;
    // 超时时间
    private int timeout;
    // 接收块大小
    private int receiveBufferSize;
    // 发送块大小
    private int sendBufferSize;

    /***
     * 与银行交易
     * 
     * @throws SocketException
     * @throws SocketTimeoutException
     */
    public Object submit(Object obj) {
        Socket socketClient = getSocket();//防止并发串报文 by lizheng
        byte[] result = null;

        if (socketClient != null) {
            try {
                result = sendAndGetDataFromBankDL(socketClient, obj);
            } catch (Exception e) {
                logger.error("SocketTransport submit:", e);
            }
        }

        return result;
    }

    /**
     * 与银行换取连接socket
     * 
     * @return
     * @throws SocketException
     * @throws SocketTimeoutException
     */
    private Socket getSocket() {
        Socket socket = null;
        try {
            socket = new Socket(host, port);
            socket.setSoTimeout(timeout);
            socket.setReceiveBufferSize(receiveBufferSize);
            socket.setSendBufferSize(sendBufferSize);
        } catch (SocketTimeoutException e) {
            logger.error("Connection scoket error:", e);
        } catch (SocketException e) {
            logger.error("Connection scoket error:", e);
        } catch (Exception e) {
            logger.error("Connection scoket error:", e);
        }
        return socket;
    }

    /**
     * socket通信 发送和接收报文
     * 
     * @param client
     * @param obj
     * @return String
     * @throws IOException
     */
    private byte[] sendAndGetDataFromBankDL(Socket client, Object obj) throws IOException {
        DataOutputStream out = null;
        byte[] bytes = new byte[2048];
        try {
            // 提交报文
            out = new DataOutputStream(client.getOutputStream());
            logger.error("SocketTransport Sent Data start: \n");
            out.write((byte[]) obj);
            out.flush();

            // 接收返回内容
            DataInputStream in = new DataInputStream(client.getInputStream());
            in.read(bytes);
            logger.error("SocketTransport the receive data length:" + bytes.length);
        } catch (IOException e) {
            logger.error("SocketTransport sendAndGetDataFromBankDL:", e);
        } finally {
            if (out != null) {
                out.close();
            }
            client.close();
        }
        return bytes;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public int getReceiveBufferSize() {
        return receiveBufferSize;
    }

    public void setReceiveBufferSize(int receiveBufferSize) {
        this.receiveBufferSize = receiveBufferSize;
    }

    public int getSendBufferSize() {
        return sendBufferSize;
    }

    public void setSendBufferSize(int sendBufferSize) {
        this.sendBufferSize = sendBufferSize;
    }

}
